package com.chainyoung.component_tmtrade.ui.activity;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.adpter.BaseFragmentAdapter;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.R2;
import com.chainyoung.component_tmtrade.bean.RegionList;
import com.chainyoung.component_tmtrade.di.component.DaggerTradeCurrencyActivityComponent;
import com.chainyoung.component_tmtrade.di.module.TradeCurrencyActivityModule;
import com.chainyoung.component_tmtrade.ui.fragment.c2c.TradeCurrencyListFragment;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeCurrencyTypeListContract;
import com.chainyoung.component_tmtrade.ui.mvp.presenter.TradeCurrencyTypeListPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.support.design.widget.TabLayout.MODE_FIXED;
import static android.support.design.widget.TabLayout.MODE_SCROLLABLE;
import static com.chainyoung.router.RouterConstants.PATH_SELECT_TRADE_MARKET;

//选择交易对
@Route(path = PATH_SELECT_TRADE_MARKET)
public class TradeCurrencyMarketListActivity extends BaseActivity<TradeCurrencyTypeListPresenter> implements TradeCurrencyTypeListContract.View {


    @BindView(R2.id.iv_cancel)
    ImageView ivCancel;

    @BindView(R2.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;


    private List<Fragment> fragmentList = new ArrayList<>();
    private String coinName;
    private boolean isFromPurse;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTradeCurrencyActivityComponent.
                builder().
                appComponent(appComponent)
                .tradeCurrencyActivityModule(new TradeCurrencyActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        coinName = getIntent().getStringExtra("coinName");
        isFromPurse=getIntent().getBooleanExtra("isFromPurse",false);
    }


    @Override
    protected void initData() {
        mPresenter.getMarketCurrencys();
    }

    @OnClick(R2.id.iv_cancel)
    void onCancel() {
        finish();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmtrade_select_trade_market;
    }


    @Override
    public void showMarketCurrencys(RegionList data) {
        List<String> titles = new ArrayList<>(data.getRegion_list());
        for (int i = 0; i < titles.size(); i++) {
            fragmentList.add(TradeCurrencyListFragment.newInstance(titles.get(i)));
        }
        tabLayout.setTabMode(titles.size() < 5 ? MODE_FIXED : MODE_SCROLLABLE);
        viewPager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList, titles));
        tabLayout.setupWithViewPager(viewPager);
        if (!TextUtils.isEmpty(coinName)) {
            for (int i = 0; i < titles.size(); i++) {
                if (coinName.equals(titles.get(i))) {
                    viewPager.setCurrentItem(i, false);
                    break;
                }
            }
        } else {
            viewPager.setCurrentItem(0, false);
        }
        viewPager.setOffscreenPageLimit(titles.size());
    }

    public boolean isFromPurse() {
        return isFromPurse;
    }
}
